<?php
class Messages extends Controller {

    function Messages() {
        parent::Controller();
        session_start();
        $this->load->model('municipalities_model');
        $this->lang->load("front");
        if (!$this->session->userdata('admin_logged_in')) redirect(base_url().'mk/admin/login', 'refresh');
    }

    function index() {
        redirect(base_url().'mk/admin/messages/listmessages', 'refresh');
    }

    function listmessages($status) {
        $this->load->helper('text');
        $this->load->model('messages_model');
        $data['allMessages'] = $this->messages_model->getAllMessagesByStatus($status);
        foreach ($data['allMessages'] as $key=>$val) {
            $data['rep_answers'][$val->message_id]=$this->messages_model->getMessageAnswers($val->message_id);
        }
        $content['head'] = $this->load->view('head',null,true);
        $content['header'] = $this->load->view('admin/header',null,true);
        $content['middleFront'] = $this->load->view('admin/list_messages',$data,true);
        $content['footer'] = $this->load->view('footer',null,true);
        $this->load->view('admin/index_front',$content);
    }

    function approveMessages($id) {
        $this->lang->load("front");
        $this->load->model('messages_model');
        $messageData=$this->messages_model->getMessageById($id);
        $data['sendData']['message_id'] = $messageData[0]->message_id;
        $data['sendData']['message'] = $messageData[0]->message;
        $data['sendData']['sender_name'] = $messageData[0]->sender_name;
        $data['sendData']['sender_address'] = $messageData[0]->sender_address;
        $data['sendData']['sender_city'] = $messageData[0]->sender_city;
        $data['sendData']['sender_email'] = $messageData[0]->sender_email;
        $data['sendData']['sender_phone'] = $messageData[0]->sender_phone;
        $data['representatives'] = $this->messages_model->getMessageAnswers($id);



        $content['head'] = $this->load->view('head',null,true);
        $content['header'] = $this->load->view('admin/header',null,true);
        $content['middleFront'] = $this->load->view('admin/approve_message',$data,true);
        $content['footer'] = $this->load->view('footer',null,true);
        $this->load->view('admin/index_front',$content);
    }

    function approval($message_id) {
        $condition = array(
            "message_id"=>(int)$message_id
        );
        $field = array(
            "message_status" => 2
        );
        $this->load->model('rep_message_model');
        $this->rep_message_model->updateStatus($condition,$field);
	
	/*	
		$this->email->to($messageData['sender_email']);
	    	$this->email->from('admin@pisiim.mk');
	    	$this->email->subject('Потврда за активација');
		$message = 'Почитувани, <br /><br />';		
		$message .= 'За да ја потврдите активацијата ве молиме кликнете на следниот линк: <br />';
		$message .= base_url().$this->lang->lang()."/messages/approvementProcess/".$message_id."/".$message_tokken.'<br /><br />';  			$message .= 'Со почит, <br /> Pisiim.mk';
		$this->email->message($message);
	    	$this->email->send();
	*/
        redirect(base_url()."mk/admin/messages/listmessages", 'refresh');
    //Ovde treba da se zemat site pretstavnici za konkretnata poraka i da im se isprati unikatniot link
    //Treba da se smeni tokenot na porakata(da se generira nov) bidejki istiot mu se prakja i na korisnikot
    //Linkot treba da bide http://localhost/writetothem/mk/messages/approvementProcess/rep_id/message_id/message_token

    }

    function deleteMessage($message_id) {
        $this->load->model('messages_model');
        $this->messages_model->deleteMessage($message_id);
        $this->load->model('rep_message_model');
        $this->rep_message_model->deleteMessage($message_id);
        redirect(base_url()."mk/admin/messages/listmessages", 'refresh');

    }

}
